package com.example.day10_contentprovider;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.provider.Telephony;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button bt_message;
    private Button bt_pictures;
    private Button bt_person;
    private Button bt_music;
    private Button bt_movie;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            requestPermissions(new String[]{
                    "android.permission.READ_CONTACTS",
                    "android.permission.WRITE_CONTACTS",
                    "android.permission.WRITE_EXTERNAL_STORAGE",
                    "android.permission.READ_EXTERNAL_STORAGE",
                    "android.permission.READ_SMS"

            },101);
        }
        insert();


    }

    private void initView() {
        bt_message = (Button) findViewById(R.id.bt_message);
        bt_pictures = (Button) findViewById(R.id.bt_pictures);
        bt_person = (Button) findViewById(R.id.bt_person);
        bt_music = (Button) findViewById(R.id.bt_music);
        bt_movie = (Button) findViewById(R.id.bt_movie);

        bt_message.setOnClickListener(this);
        bt_pictures.setOnClickListener(this);
        bt_person.setOnClickListener(this);
        bt_music.setOnClickListener(this);
        bt_movie.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_message:
                message();
                break;
            case R.id.bt_pictures:
                pictures();
                break;
            case R.id.bt_person:
                person();
                break;
            case R.id.bt_music:
                music();
                break;
            case R.id.bt_movie:
                movie();
                break;
        }
    }

    private void movie() {
        //TODO 1：获得ContentResolver
        ContentResolver contentResolver = getContentResolver();
        //TODO 2:查询所有视频
        Cursor cursor = contentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                null, null, null,null);
        //TODO 3:遍历解析获取字段
        if(cursor != null){
            while (cursor.moveToNext()){
                String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                String artist = cursor.getString(cursor.getColumnIndex( MediaStore.Audio.Media.ARTIST));
                String duration = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
                String data = cursor.getString(cursor.getColumnIndex( MediaStore.Audio.Media.DATA));
                String size = cursor.getString(cursor.getColumnIndex( MediaStore.Audio.Media.SIZE));
                Log.d("ytx", "视频名称: "+title+"  创作者："+artist+"   总时长："+duration+"   地址："+data+"   大小："+size);
            }

        }


    }

    private void music() {
        //TODO 1：获得ContentResolver
        ContentResolver contentResolver = getContentResolver();
        //TODO 2:查询所有的音乐
        Cursor cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                null, null, null,null);
        //TODO 3:遍历解析获取字段
        if(cursor != null){
            while (cursor.moveToNext()){
                String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
                String artist = cursor.getString(cursor.getColumnIndex( MediaStore.Audio.Media.ARTIST));
                String duration = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
                String data = cursor.getString(cursor.getColumnIndex( MediaStore.Audio.Media.DATA));
                String size = cursor.getString(cursor.getColumnIndex( MediaStore.Audio.Media.SIZE));
                Log.d("ytx", "歌名: "+title+"歌手："+artist+"总时长"+duration+"地址"+data+"大小："+size);
            }
        }
    }

    private void person() {
        //TODO 1：获得ContentResolver
        ContentResolver contentResolver = getContentResolver();
        //TODO 2:查询所有的联系人
        Cursor cursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null, null, null,null);
        //TODO 3:遍历解析获取字段
        if(cursor != null){
            while (cursor.moveToNext()){
                String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                Log.d("ytx", "姓名: "+name+"电话："+number);
            }

        }


    }

    private void insert(){
        ContentValues values = new ContentValues();
//首先向RawContacts.CONTENT_URI执行一个空值插入，目的是获取系统返回的rawContactId
        Uri rawContactUri = getContentResolver().insert(ContactsContract.RawContacts.CONTENT_URI, values);
        long rawContactId = ContentUris.parseId(rawContactUri);
//往data表入姓名数据
        values.clear();
        values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
        values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);//内容类型
        values.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, "李天山");
        getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);
//往data表入电话数据
        values.clear();
        values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
        values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
        values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, "13921009789");
        getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);

    }

    private void delete(){
        ContentValues values = new ContentValues();
//首先向RawContacts.CONTENT_URI执行一个空值插入，目的是获取系统返回的rawContactId
        Uri rawContactUri = getContentResolver().insert(ContactsContract.RawContacts.CONTENT_URI, values);
        long rawContactId = ContentUris.parseId(rawContactUri);
//往data表入姓名数据
        values.clear();
        values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
        values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);//内容类型
        values.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, "李天山");
        getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);
//往data表入电话数据
        values.clear();
        values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
        values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
        values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, "13921009789");
        getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);

    }


    private void pictures() {
        //TODO 1：获得ContentResolver
        ContentResolver contentResolver = getContentResolver();
        //TODO 2:查询所有图片
        Cursor cursor = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                null, null, null,null);
        //TODO 3:遍历解析获取字段
        if(cursor != null){
            while (cursor.moveToNext()){
                String title = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.TITLE));
                String data = cursor.getString(cursor.getColumnIndex( MediaStore.Images.Media.DATA));
                String size = cursor.getString(cursor.getColumnIndex( MediaStore.Images.Media.SIZE));
                String height = cursor.getString(cursor.getColumnIndex( MediaStore.Images.Media.HEIGHT));
                String width = cursor.getString(cursor.getColumnIndex(  MediaStore.Images.Media.WIDTH));
                Log.d("ytx", "图片名称: "+title+"  高度："+height+"   宽度："+width+"   地址："+data+"   大小："+size);
            }
        }
    }

    private void message() {
        //TODO 1：获得ContentResolver
        ContentResolver contentResolver = getContentResolver();
        //TODO 2:查询所有短信
        Cursor cursor = contentResolver.query(Telephony.Sms.CONTENT_URI,
                null, null, null,null);
        //TODO 3:遍历解析获取字段
        if(cursor != null){
            while (cursor.moveToNext()){
                String address = cursor.getString(cursor.getColumnIndex(Telephony.Sms.ADDRESS));
                String body = cursor.getString(cursor.getColumnIndex( Telephony.Sms.BODY));
                Log.d("ytx", "短信地址: "+address+"  短信内容："+body);
            }
        }


    }

}
